/**
 * Created by hanj on 2021/1/29 16:36
 */
package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 使用easyExcel导出excel
 */
@RestController
public class ExportExcelDemo {

    /**
     * 使用EasyExcel模板导出excel
     * @param response
     * @param session
     * @throws Exception
     */
    @GetMapping(value = "/exportExcelByTemplate")
    public void exportExcelByTemplate(HttpServletResponse response, HttpSession session) throws Exception {
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream;charset=utf-8");
        LocalDate time = LocalDate.now();
        String fileName = URLDecoder.decode("easyExcel导出模板_" + time, "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls" + "\"");
        OutputStream out = response.getOutputStream();
        List<Student> data = getData();

        //根据模板导出数excel
        ExcelUtil.exportTemplate("templates/template.xls", "model.xls", out, Student.class,ExportExcelDemo.class, data, session);
    }


    /**
     * 使用EasyExcel导出excel
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream;charset=utf-8");
        LocalDate time = LocalDate.now();
        String fileName = URLDecoder.decode("easyExcel导出_" + time, "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls" + "\"");
        OutputStream out = response.getOutputStream();
        List<Student> data = getData();
        Map<Integer,String[]> map = new HashMap<>();
        map.put(2,new String[]{"男","女"});//下拉导出
        ExcelUtil.exportBySelect(out,data,map);
        out.close();
    }

    /**
     * 使用EasyExcel导出excel
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/exportExcelByWaterMark")
    public void exportExcelByWaterMark(HttpServletResponse response) throws Exception {
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream;charset=utf-8");
        LocalDate time = LocalDate.now();
        String fileName = URLDecoder.decode("easyExcel导出_" + time, "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls" + "\"");
        OutputStream out = response.getOutputStream();
        List<Student> data = getData();
        ExcelUtil.exportBySelectHandler(out,data,"lalalalala");
        out.close();
    }

    /**
     * 使用EasyExcel填充导出excel
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/exportExcelByFill")
    public void exportExcelByFill(HttpServletResponse response, HttpSession session) throws Exception {
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream;charset=utf-8");
        LocalDate time = LocalDate.now();
        String fileName = URLDecoder.decode("easyExcel导出模板_" + time, "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls" + "\"");
        OutputStream out = response.getOutputStream();
        //封装需要导出的数据
        List<Teacher> teacherList = getDataTeacher();
        //封装需要特殊处理的数据
        Map<String,String> data = new HashMap<>();
        data.put("year","2022");

        //根据模板导出数excel
        ExcelUtil.exportTemplateByFill("templates/teacherTemplate.xls", "teacherModel.xls", out, Student.class,ExportExcelDemo.class, teacherList,data, session);
    }


    /**
     * 使用EasyExcel填充导出多sheet页的excel
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/exportSheetsExcelByFill")
    public void exportSheetsExcelByFill(HttpServletResponse response, HttpSession session) throws Exception {
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream;charset=utf-8");
        LocalDate time = LocalDate.now();
        String fileName = URLDecoder.decode("easyExcel导出模板_" + time, "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls" + "\"");
        OutputStream out = response.getOutputStream();
        //封装多个sheet页的数据
        List<Teacher> teacherList = getDataTeacher();
        List<Student> studentList = getData();
        Map<String,List> exportData = new HashMap<>();
        exportData.put("0",teacherList);
        exportData.put("1",studentList);
        //封装需要填充的特殊数据
        Map<String,String> data = new HashMap<>();
        data.put("year","2022");

        //根据模板导出数excel
        ExcelUtil.exportSheetsTemplateByFill("templates/schoolTemplate.xls", "schoolModel.xls", out,ExportExcelDemo.class, exportData,data, session);
    }

//    /**
//     * 使用poi导出excel
//     *
//     * 两个不同的学生成绩表，成绩相同的学生底色相同
//     * @param response
//     * @throws Exception
//     */
//    @GetMapping(value = "/exportPoiExcel")
//    public void exportPoiExcel(HttpServletResponse response) throws Exception {
//        String title = "学生成绩对比表";
//        String[] headers1 = {"姓名","年龄","性别","学号","家庭地址","成绩"};
//        String[] headers2 = {"姓名","性别","性别","成绩"};
//        ExcelUtil.fillExcelData(getData(), getData1(), headers1, headers2, title, response);
//
//    }


    //封装导出数据
    private List<Student> getData() {
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Student student = new Student();
            student.setName("测试学生姓名-" + i);
            student.setAge(i);
            student.setSex(i % 2 == 1 ? "男" : "女");
            student.setNum(111111L);
            student.setAddress("追晨始梦CSDN博客");
            student.setFraction(i+1);
            list.add(student);
        }
        return list;
    }

    //封装导出数据
    private List<Student2> getData1() {
        List<Student2> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Student2 student = new Student2();
            student.setName("测试学生2姓名-" + i);
            student.setAge(i);
            student.setSex(i % 2 == 1 ? "男" : "女");
            student.setFraction(i+i);
            list.add(student);
        }
        return list;
    }

    //封装导出数据
    private List<Teacher> getDataTeacher() {
        List<Teacher> list = new ArrayList<>();
        Teacher teacher = new Teacher();
        teacher.setName("数学老师");
        teacher.setSex("女");
        teacher.setNum("8888888");
        teacher.setSubject("数学");
        teacher.setTeacherYear(5);
        list.add(teacher);
        Teacher teacher1 = new Teacher();
        teacher1.setName("编程老师");
        teacher1.setSex("男");
        teacher1.setNum("6666666");
        teacher1.setSubject("编程");
        teacher1.setTeacherYear(100);
        list.add(teacher1);
        return list;
    }
}
